package com.charm.logininfo.ui

import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.transition.Explode
import android.view.View
import android.view.ViewAnimationUtils
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.Button
import android.widget.EditText
import android.widget.RelativeLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.charm.codes.mvp.factory.CreatePresenter
import com.charm.codes.mvp.view.AbstractMvpAppCompatActivity
import com.charm.logininfo.R
import com.charm.logininfo.contract.LoginView
import com.charm.logininfo.contract.presenter.LoginPresenter

/**
 * Created by tools on 2018/1/16.
 */
@Route(path = "/logininfo/login")
@CreatePresenter(LoginPresenter::class)
class LoginActivity : AbstractMvpAppCompatActivity<LoginView, LoginPresenter>(), LoginView {

    private var etUsername: EditText? = null
    private var etPassword: EditText? = null
    private var btGo: Button? = null
    private var cv: CardView? = null
    private var fab: FloatingActionButton? = null
    private lateinit var container: RelativeLayout
    var revealWidth: Int = 0
    var revealHeight: Int = 0
    var isReveal = true
    protected override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        revealHeight = intent.extras["revealHeight"] as Int
        revealWidth = intent.extras["revealWidth"] as Int
        initView()
        setListener()
    }

    private fun onStartRevealAnimation() {
        val finalRadius = Math.hypot(container.width.toDouble(), container.height.toDouble()).toFloat()
        val anim = ViewAnimationUtils.createCircularReveal(container, container.width, container.height, 0f, finalRadius)
        container.setBackgroundColor(ContextCompat.getColor(this, R.color.color_FFCC00))
        anim.duration = 1000
        anim.interpolator = AccelerateDecelerateInterpolator()
        anim.start()
    }

    private fun initView() {
        container = findViewById(R.id.container)
        etUsername = findViewById(R.id.et_username)
        etPassword = findViewById(R.id.et_password)
        btGo = findViewById(R.id.bt_go)
        cv = findViewById(R.id.cv)
        fab = findViewById(R.id.fab)
    }

    private fun setListener() {
        container.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            if (isReveal) {
                onStartRevealAnimation()
                isReveal = false
            }
        }
        btGo!!.setOnClickListener {
            val explode = Explode()
            explode.duration = 500
            window.exitTransition = explode
            window.enterTransition = explode
            val oc2 = ActivityOptionsCompat.makeSceneTransitionAnimation(this@LoginActivity)
        }
        fab!!.setOnClickListener {
            window.exitTransition = null
            window.enterTransition = null
            val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this@LoginActivity, fab!!, fab!!.transitionName)
            ARouter.getInstance()
                    .build("/logininfo/register")
                    .withOptionsCompat(options)
                    .navigation(this@LoginActivity)
        }
    }


    protected override fun onRestart() {
        super.onRestart()
        fab!!.visibility = View.GONE
    }

    protected override fun onResume() {
        super.onResume()
        fab!!.visibility = View.VISIBLE
    }


    override fun finish() {
        super.finish()
    }
}